﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class DanWeiBianJi : BaseForm
    {
        private DataGridViewRow m_SelectRow = null;
        private bool m_IsRuKu = false;

        public DanWeiBianJi(bool IsRuKu)
        {
            InitializeComponent();
            m_IsRuKu = IsRuKu;
        }

        public DanWeiBianJi(DataGridViewRow SelectRow,bool IsRuKu)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
            m_IsRuKu = IsRuKu;
        }

        private void DanWeiBianJi_Load(object sender, EventArgs e)
        {
            BindGongYingShang();

            if (m_IsRuKu)
            {
                cmb_danweileixing.SelectedIndex = 0;
            }
            else
            {
                cmb_danweileixing.SelectedIndex = 1;
            }
            if (m_SelectRow != null)
            {
                txt_danweimingcheng.Text = string.Format("{0}", m_SelectRow.Cells["col_danweimingcheng"].Value);
                cmb_danweileixing.SelectedItem = string.Format("{0}", m_SelectRow.Cells["col_danweileixing"].Value);
                chk_danweizhuangtai.Checked = m_SelectRow.Cells["col_danweizhuangtai"].Value.Equals("启用") ? true : false;
                cmb_danweileixing.SelectedItem = string.Format("{0}", m_SelectRow.Cells["col_danweileixing"].Value);
                string gongYingShang = string.Format("{0}", m_SelectRow.Cells["col_gongyingshangbianhao"].Value);
                if (!string.IsNullOrEmpty(gongYingShang))
                {
                    cmb_gongyingshang.SelectedValue = gongYingShang;
                }
            }
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txt_danweimingcheng.Text))
            {
                KeaoUtils.ShowInfoMessage("单位名称不能为空，请重试。");
                return;
            }

            if (m_SelectRow == null)
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("danweimingcheng", DataValueCommonType.字符串, txt_danweimingcheng.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, cmb_danweileixing.Text, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", createCondition);
                if (Dt == null)
                {
                    return;
                }
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("单位名称重复，请重试。");
                    return;
                }

                TableRow newRow = new TableRow("tbl_danwei", KeaoUtils.GetGUID(), DBCtl.插入);
                newRow.DataCols.Add(new TableCol() { ColName = "danweimingcheng", ValueMember = txt_danweimingcheng.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "danweizhuangtai", ValueMember = chk_danweizhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "danweileixing", ValueMember = cmb_danweileixing.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                if (cmb_gongyingshang.Text == "—请选择—")
                {
                    newRow.DataCols.Add(new TableCol() { ColName = "gongyingshang", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gongyingshangmingcheng", ValueMember = "", ValueType = DataValueCommonType.字符串 });

                }
                else
                {
                    newRow.DataCols.Add(new TableCol() { ColName = "gongyingshang", ValueMember = cmb_gongyingshang.SelectedValue, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gongyingshangmingcheng", ValueMember = cmb_gongyingshang.Text, ValueType = DataValueCommonType.字符串 });
                }
                if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("danweimingcheng", DataValueCommonType.字符串, txt_danweimingcheng.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, cmb_danweileixing.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_id"].Value),"", SearchConditionType.不等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", createCondition);
                if (Dt == null)
                {
                    return;
                }
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("该单位名称已经存在，请重试。");
                    return;
                }

                TableRow editRow = new TableRow("tbl_danwei", string.Format("{0}",m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                editRow.DataCols.Add(new TableCol() { ColName = "danweimingcheng", ValueMember = txt_danweimingcheng.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "danweizhuangtai", ValueMember = chk_danweizhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "danweileixing", ValueMember = cmb_danweileixing.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                if (cmb_gongyingshang.Text == "—请选择—")
                {
                    editRow.DataCols.Add(new TableCol() { ColName = "gongyingshang", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gongyingshangmingcheng", ValueMember = "", ValueType = DataValueCommonType.字符串 });

                }
                else
                {
                    editRow.DataCols.Add(new TableCol() { ColName = "gongyingshang", ValueMember = cmb_gongyingshang.SelectedValue, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gongyingshangmingcheng", ValueMember = cmb_gongyingshang.Text, ValueType = DataValueCommonType.字符串 });
                }
                if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void BindGongYingShang()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("zhuantai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_gongyingshang");
            DataRow newRow = Dt.NewRow();
            newRow["mingcheng"] = "—请选择—";
            newRow["id"] = "";
            Dt.Rows.InsertAt(newRow, 0);
            cmb_gongyingshang.DisplayMember = "mingcheng";
            cmb_gongyingshang.ValueMember = "id";
            cmb_gongyingshang.DataSource = Dt;
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
